package com.beordie.version_2.aop;

import com.beordie.version_2.aop.aspect.BODAdvice;
import com.beordie.version_2.aop.interceptor.BODMethodInvocation;
import com.beordie.version_2.aop.support.BODAdviseSupport;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Date 2022/2/19 9:34
 * @Created 30500
 */
public class BODJdkDynamicAopProxy implements BODAopProxy, InvocationHandler {
    private BODAdviseSupport advices;

    public BODJdkDynamicAopProxy(BODAdviseSupport advices) {
        this.advices = advices;
    }

    @Override
    public Object getProxy() {
        BODAdviseSupport con = this.advices;
        return Proxy.newProxyInstance(
                this.advices.getTargetClass().getClassLoader(), this.advices.getTargetClass().getInterfaces(), this);
    }

    @Override
    public Object getProxy(ClassLoader classLoader) {
        return null;
    }

    @Override
    public Object invoke(Object o, Method method, Object[] objects) throws Throwable {
        List<Object> chain = this.advices.getInterceptorsAndDynamicInterceptionAdvice(method, this.advices.getTargetClass());
        BODMethodInvocation invocation = new BODMethodInvocation(o, this.advices.getTargetInstance(), method, objects, this.advices.getTargetClass(), chain);
        return invocation.procced();
    }
}
